/*
** ############################################################################
**     Project   : OSdee - Operative Systems Design for Embedded Envrionments
**     File      : OS_Tasks.c
**     Revision  ; 1.0
**     Abstract  :
**         Scheduled Tasks implementation.
**
**     Copyright : 2014 Fernando Rodriguez - Victor Huidobro, 
**                 Open source with out any responsability.
**     
**     mail      : frr@gmail.com, vhuidobrog@gmail.com
** ############################################################################
*/

#include "OS_Task.h"
#include <MC9S12XEP100.h>

/*
** ===================================================================
**     Function    :  OS_Task__X
**
**     Description :
**         OS Task callbacks implementations.
**         User can define his/her own callback implementation
**         for any channel, using this prototypes or maybe 
**         any other function linked to the task in the configuration.
** ===================================================================
*/

/* High Work load for each task - Generates OS_LIMIT due multimple activations
** When overlapping Tas1, Task2, and Task5 the total execution time exceeds Task1 deadline (~950us)

#define OS_TASK_1_WORK_LOAD    400 // Execution Time ~67us 
#define OS_TASK_2_WORK_LOAD    500 // Execution Time ~84us 
#define OS_TASK_3_WORK_LOAD    600 // Execution Time ~100us 
#define OS_TASK_4_WORK_LOAD    700 // Execution Time ~117us 
#define OS_TASK_5_WORK_LOAD    800 // Execution Time ~134us 
#define OS_TASK_6_WORK_LOAD    900 // Execution Time ~150us 
#define OS_TASK_7_WORK_LOAD   1000 // Execution Time ~167us 
*/

/* Low Work load for each task - Generates OS_LIMIT due multimple activations
** Avoid overlapping Tas1, Task2, and Task5 the total execution does not exceed Task1 deadline (~950us)
*/
#define OS_TASK_1_WORK_LOAD    100 // Execution Time ~17us 
#define OS_TASK_2_WORK_LOAD    125 // Execution Time ~21us 
#define OS_TASK_3_WORK_LOAD    150 // Execution Time ~25us 
#define OS_TASK_4_WORK_LOAD    175 // Execution Time ~30us 
#define OS_TASK_5_WORK_LOAD    200 // Execution Time ~34us 
#define OS_TASK_6_WORK_LOAD    22500 // Execution Time ~38us 
#define OS_TASK_7_WORK_LOAD    250 // Execution Time ~42us 


extern teE_OS_ERR TerminateTask(void);
extern void SchM_OsTick(void);

void vOS_Task_Work(UINT16 u16WorkLoad, UINT8 u8PortMask)
{
  UINT16 u16count = 0;
  
  PORTB |= u8PortMask /*| 0xFE*/;       /*Set new task bit and clear background task bit.*/  
  while(u16count++ < u16WorkLoad);
  PORTB &= ~u8PortMask;
}


TASK(vTask_1)
{ 
  UINT16 u16count = 0;
  UINT16 u16WorkLoad = OS_TASK_1_WORK_LOAD;
  UINT8 u8PortMask = PORTB_PB1_MASK;
  for(;;){
      PORTB |= u8PortMask /*| 0xFE*/;       /*Set new task bit and clear background task bit.*/  
      while(u16count++ < u16WorkLoad);
            PORTB &= ~u8PortMask;
            u16count=0;  
  }
}

TASK(vTask_2)
{
  SchM_OsTick();
  (void)TerminateTask();
  DispatcherService();
  
}

TASK(vTask_3)
{
  vOS_Task_Work(OS_TASK_3_WORK_LOAD, PORTB_PB3_MASK);
  
  (void)ActivateTask(OS_Task_4);
  __asm( "swi" );
  
  vOS_Task_Work(OS_TASK_3_WORK_LOAD, PORTB_PB3_MASK);
  
  (void)TerminateTask();
  DispatcherService();
}

TASK(vTask_4)
{
  vOS_Task_Work(OS_TASK_4_WORK_LOAD, PORTB_PB4_MASK);
  (void)TerminateTask();
  DispatcherService();
}

TASK(vTask_5)
{
  vOS_Task_Work(OS_TASK_5_WORK_LOAD, PORTB_PB5_MASK);
  (void)TerminateTask();
  DispatcherService();
}

TASK(vTask_6)
{
  vOS_Task_Work(OS_TASK_6_WORK_LOAD, PORTB_PB6_MASK);
  (void)TerminateTask();
  DispatcherService();
}

TASK(vTask_7)
{
  vOS_Task_Work(OS_TASK_7_WORK_LOAD, PORTB_PB7_MASK);
  
  (void)ActivateTask(OS_Task_3);
  __asm( "swi" );
  
  (void)TerminateTask();
  DispatcherService();
}
